<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title')->comment('帖子标题');
            $table->text('content')->comment('帖子内容（支持Markdown）');
            $table->unsignedBigInteger('user_id')->comment('作者ID');
            $table->unsignedBigInteger('bar_id')->comment('所属贴吧ID');
            $table->json('images')->nullable()->comment('帖子图片（JSON数组）');
            $table->unsignedInteger('views')->default(0)->comment('浏览数');
            $table->unsignedInteger('like_count')->default(0)->comment('点赞数');
            $table->unsignedInteger('reply_count')->default(0)->comment('回复数');
            $table->boolean('is_pinned')->default(false)->comment('是否置顶');
            $table->boolean('is_essence')->default(false)->comment('是否精华');
            $table->timestamp('pinned_at')->nullable()->comment('置顶时间');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('bar_id')->references('id')->on('bars')->onDelete('cascade');
            $table->index('bar_id');
            $table->index('user_id');
            $table->index('created_at');
            $table->index(['bar_id', 'is_pinned', 'created_at']);
            $table->index(['bar_id', 'is_essence', 'created_at']);
            $table->index(['bar_id', 'like_count', 'created_at']); // 热度排序
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('posts');
    }
};
